<template>
  <section class="my-flea">
    <div class="choose flex flex-between flex-middle" @click="visibleArea = !visibleArea">
      <div class="flex flex-middle">
        <img src="/static/images/city-icon.png" alt />
        <div>
          <div class="adr">{{areaId?areaId.name:'选择城市'}}</div>
          <div class="desc mt10">选择城市</div>
        </div>
      </div>
      <van-icon name="arrow" class="icon" color="#C1C4C9" />
    </div>
    <van-action-sheet :show="visibleArea" @close="visibleArea = false">
      <van-area
        :area-list="areaList"
        @cancel="visibleArea = !visibleArea"
        @confirm="changeCity"
        :columns-num="2"
        title="选择城市"
      />
    </van-action-sheet>
    <div class="choose flex flex-between flex-middle" @click="chooseSch">
      <div class="flex flex-middle">
        <img src="/static/images/school-icon.png" alt />
        <div>
          <div class="adr">{{school?school.schoolName:'选择学校'}}</div>
          <div class="desc mt10">选择学校</div>
        </div>
      </div>
      <van-icon name="arrow" class="icon" color="#C1C4C9" />
    </div>
    <van-action-sheet :show="visibleSchool" @close="visibleSchool = false">
      <van-picker
        :columns="schoolList"
        value-key="schoolName"
        show-toolbar
        title="选择学校"
        @cancel="visibleSchool = !visibleSchool"
        @confirm="schChange"
      />
    </van-action-sheet>
    <div
      class="task flex flex-between"
      @click="routerTo(item.productId)"
      v-for="item in list"
      :key="item"
    >
      <div class="left flex">
        <img :src="item.frontImgUrl" />
        <div class="flex-col flex-between content-text">
          <div class="title">{{item.productName}}</div>
          <div class="status">{{typeOption[item.quality]}}</div>
          <div class="price">￥{{item.price}}</div>
        </div>
      </div>
      <div class="right flex-col flex-bottom">
        <div class="status">{{item.displaceGoodsClass == 1 ? '置换中' : '出售中'}}</div>
        <div class="mt15 num">X{{item.nums}}</div>
      </div>
    </div>
    <van-toast id="van-toast" />
  </section>
</template>
<script>
import areaList from "../../../common/js/city.js";
export default {
  data() {
    return {
      areaList,
      visibleArea: false,
      areaId: "", //区域{code,name }
      visibleSchool: false,
      school: "",
      schoolList: "",
      list: "",
      typeOption: {
        1: '全新',
        2: '九成新',
        3: '八成新',
        4: '七成新',
        5: '六成新',
        6: '五成新'
      }
    };
  },
  onLoad() {
    Object.assign(this.$data, this.$options.data());
    const jobCity = this.$getStorage('jobCity');
    if (jobCity) {
      this.areaId = jobCity;
      this.$nextTick(() => {
        this.getSchoolListByArea(1);
      });
    }
  },
  methods: {
    routerTo(id) {
      wx.navigateTo({ url: "../fleadetail/main?id=" + id });
    },
    chooseSch() {
      if (!this.areaId) {
        wx.showToast({
          title: "请先选择城市", //提示的内容,
          icon: "none", //图标,
          duration: 2000, //延迟时间,
          mask: true, //显示透明蒙层，防止触摸穿透,
          success: res => {}
        });
      } else {
        this.visibleSchool = !this.visibleSchool;
      }
    },
    //选择学校确认
    schChange(e) {
      this.school = e.target.value;
      this.$setStorage('schoolInfo', e.target.value);
      this.fleaList();
      this.visibleSchool = !this.visibleSchool;
    },
    changeCity(e) {
      this.areaId = e.target.values[1];
      this.$setStorage('jobCity', e.target.values[1]);
      this.getSchoolListByArea();
      this.visibleArea = !this.visibleArea;
    },
    //区域选择学校列表
    getSchoolListByArea(type) {
      this.$api.getSchoolListByArea({ areaId: this.areaId.code }).then(res => {
        this.schoolList = res.data.schoolList;
        if (type === 1) {
          this.$nextTick(() => {
            const schoolInfo = this.$getStorage('schoolInfo');
            this.school = schoolInfo;
            this.$nextTick(() => {
              this.fleaList();
            });
          })
        }
      });
    },
    //跳蚤市场列表
    fleaList() {
      this.$api.fleaList({ schoolId: this.school.schoolId }).then(res => {
        this.list = res.data.productList;
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.my-flea {
  .choose {
    margin: 15px;
    padding: 15px;
    background: rgba(255, 255, 255, 1);
    border-radius: 4px;
    img {
      width: 36px;
      height: 36px;
      margin-right: 15px;
    }
    .icon {
      width: 9px;
      height: 15px;
    }
    .adr {
      font-size: 13px;
      font-family: PingFang;
      font-weight: 500;
      color: rgba(46, 46, 46, 1);
    }
    .desc {
      font-size: 12px;
      font-family: PingFang;
      font-weight: 500;
      color: rgba(193, 196, 201, 1);
    }
  }
  .task {
    background: white;
    padding: 25px 15px;
    margin: 15px;
    background: rgba(255, 255, 255, 1);
    border-radius: 4px;
    .left {
      img {
        width: 70px;
        height: 70px;
        border-radius: 2px;
        margin-right: 10px;
      }
      .titile {
        font-size: 15px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(46, 46, 46, 1);
        line-height: 21px;
      }
      .status {
        font-size: 12px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(193, 196, 201, 1);
        width: 45px;
        height: 18px;
        line-height: 18px;
        text-align: center;
        background: rgba(247, 247, 247, 1);
        border-radius: 2px;
      }
      .price {
        font-size: 15px;
        font-family: PingFang;
        font-weight: 400;
        color: rgba(255, 156, 0, 1);
        line-height: 22px;
      }
    }
    .right {
      width: 100px;
      .status {
        padding: 3px 6px;
        border: 1px solid rgba(59, 184, 253, 1);
        border-radius: 2px;
        font-size: 12px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(59, 184, 253, 1);
      }
      .num {
        font-size: 15px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(193, 196, 201, 1);
        line-height: 36px;
      }
    }
  }
}
.content-text {
  flex: 1;
}
</style>
<style lang="scss">
page {
  background: #f7f7f7;
}
</style>